﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.BLL;
using Cisoft.Booking.Model;
using Cisoft.IntegrationSystem.Common;

public partial class Business_Messages : WebUI.Page.ManagePage
{
    private int pageCount = -1;
    private ShortMessageBO smBO = new ShortMessageBO();
    private ShortMessageMO smMO = new ShortMessageMO();

    #region 页面加载事件
    /// <summary>
    /// 页面加载事件
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindHour();
            //GetList();
            txtDateA.Text = DateTime.Now.ToShortDateString();
            txtDateB.Text = DateTime.Now.ToShortDateString();
            ddl_StartHour.SelectedIndex = 0;
            ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
            string startTime = DateTime.Now.ToShortDateString() + " 00:00:00";
            string endTime = DateTime.Now.ToShortDateString() + " 23:59:59";
            DataSet ds = smBO.GetList(" ShortMessageSendTime between '" + startTime + "' and '" + endTime + "' ");
            Cache["SMSList"] = ds.Tables[0];
            Bind(Cache["SMSList"] as DataTable, GridView1);
        }
    }
    #endregion

    #region 显示数据 private void GetList()
    /// <summary>
    /// 显示数据
    /// </summary>
    private void GetList()
    {
        DataSet ds = smBO.GetAllList();
        DataView dv = ds.Tables[0].DefaultView;
        dv.Sort = " ShortMessageSendTime desc";

        GridView1.DataSource = dv.ToTable();//ds.Tables[0].DefaultView.Sort;
        pageCount = ds.Tables[0].Rows.Count;

        if (pageCount > 0)
        {
            ViewState["numbers"] = pageCount;
            GridView1.DataBind();
            PageStatus();
        }
        else
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
            ViewState["numbers"] = "0";
            PageStatus();
        }
    }

    /// <summary>
    /// 数据绑定并分页
    /// </summary>
    /// <param name="dt">数据源DataTable</param>
    /// <param name="gv">绑定的控件GridView</param>
    private void Bind(DataTable dt, GridView gv)
    {

        pageCount = dt.Rows.Count;

        if (pageCount > 0)
        {
            ViewState["numbers"] = pageCount;
            Cache["pagenumbers"] = pageCount;
            DataView dv = dt.DefaultView;
            dv.Sort = "ShortMessageSendTime desc";
            gv.DataSource = dv.ToTable();
            gv.DataBind();
            //div_pager.Visible = true;
            butState(gv, btnFirst, btnPrev, btnNext, btnLast);
            ShowStats(gv, lblCurrentIndex, lblNumbers, lblPageCount, "条短信记录", Cache["pagenumbers"].ToString());
        }
        else
        {
            gv.DataSource = null;
            gv.DataBind();
            ViewState["numbers"] = "0";
            Cache["pagenumbers"] = 0;
            butState(gv, btnFirst, btnPrev, btnNext, btnLast);
            ShowStats(gv, lblCurrentIndex, lblNumbers, lblPageCount, "条短信记录", Cache["pagenumbers"].ToString());
            //div_pager.Visible = false;
        }
    }
    #endregion

    #region 翻页事件
    /// <summary>
    /// 翻页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void PagerButtonClick(object sender, EventArgs e)
    {
        PagerEvent(sender, e, GridView1);
        //GetList();
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion

    #region gridview中的事件

    #region PageIndexChanging事件
    /// <summary>
    /// PageIndexChanging事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
    }
    #endregion

    #region DataBound
    protected void GridView1_DataBound(object sender, EventArgs e)
    {

    }
    #endregion

    #region RowCommand
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Edit(sender, e);
    }
    #endregion

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //鼠标在列表行中停留时的样式
        for (int i = -1; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#B5C9DD'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        //显示行号
        AddNo(GridView1, "lbl_No");
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
            //显示时间
            Label lbl_ShortMessageSendTime = (Label)e.Row.FindControl("lbl_ShortMessageSendTime");
            lbl_ShortMessageSendTime.Text = Convert.ToDateTime(lbl_ShortMessageSendTime.Text).GetDateTimeFormats('f')[0].ToString();

            //显示网上预约类型
            Label lbl_ShortMessageType = (Label)e.Row.FindControl("lbl_ShortMessageType");
            switch (lbl_ShortMessageType.Text)
            {
                case "YD":
                    lbl_ShortMessageType.Text = "网上预约";
                    break;
                case "DC":
                    lbl_ShortMessageType.Text = "网上预约";
                    break;
                case "TP":
                    lbl_ShortMessageType.Text = "网上预约";
                    break;
            }

            CustomersBO cBO = new CustomersBO();
            CustomersMO cMO = new CustomersMO();
            Label lbl_ShortMessageRecive = e.Row.FindControl("lbl_ShortMessageRecive") as Label;
            string reciveNew = "";
            foreach (string s in lbl_ShortMessageRecive.Text.TrimEnd(',').Split(','))
            { 
                try
                {
                    string ss = cBO.GetNameByMobile(s);
                    if(!string.IsNullOrEmpty(ss))
                    {
                        reciveNew += ss + "/" + s + ",";
                    }
                }
                catch
                {}

            }
            reciveNew = reciveNew.TrimEnd(',');
            if (reciveNew.Length > 20)
            {
                lbl_ShortMessageRecive.ToolTip = reciveNew;
                lbl_ShortMessageRecive.Text = reciveNew.Substring(0, 18) + "...";
            }

            Label lbl_ShortMessageSend = e.Row.FindControl("lbl_ShortMessageSend") as Label;
            try
            {
                string ss = cBO.GetNameByMobile(lbl_ShortMessageSend.Text);
                if(!string.IsNullOrEmpty(ss))
                {
                    lbl_ShortMessageSend.Text = ss + "/" + lbl_ShortMessageSend.Text;
                }
            }
            catch
            { }
        }   
    }

    #endregion

    #region 修改事件
    private void Edit(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "eidt")
        {
            Response.Redirect("MessageDetail.aspx?ShortMessageID=" + e.CommandArgument);
        }
    }
    #endregion

    #region 查询事件
    protected void butlookout_Click(object sender, ImageClickEventArgs e)
    {
        string startTime = string.IsNullOrEmpty(txtDateA.Text) ? "" : txtDateA.Text + " " + ddl_StartHour.SelectedValue + ":00";
        string endTime = string.IsNullOrEmpty(txtDateB.Text) ? "" : txtDateB.Text + " " + ddl_EndHour.SelectedValue + ":00";
        string txtField = string.IsNullOrEmpty(txtPropasal.Text) ? "" : txtPropasal.Text;
        //如果查询全部
        if (ddl_QueryType.SelectedIndex == 0)
        {
            DataSet ds = smBO.GetQueryList("ShortMessageSendTime", startTime, endTime, ddl_SendType.SelectedValue, txtField);
            Cache["SMSList"] = ds.Tables[0];
            Bind(Cache["SMSList"] as DataTable, GridView1);
        }
        //如果按短信类型查询
        else
        {
            DataSet ds = smBO.GetQueryList("ShortMessageSendTime", startTime, endTime, ddl_SendType.SelectedValue, txtField, "ShortMessageType", ddl_QueryType.SelectedValue);
            Cache["SMSList"] = ds.Tables[0];
            Bind(Cache["SMSList"] as DataTable, GridView1);
        }
    }
    #endregion

    #region 按钮和翻页事件 void PageStatus()
    private void PageStatus()
    {
        butState(GridView1, btnFirst, btnPrev, btnNext, btnLast);
        ShowStats(GridView1, lblCurrentIndex, lblNumbers, lblPageCount);
    }
    #endregion


    #region 昨天的短信记录
    protected void btn_LastDay_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTime.Now.AddDays(-1).ToShortDateString();
        txtDateB.Text = DateTime.Now.AddDays(-1).ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTime.Now.AddDays(-1).ToShortDateString() + " 00:00:00";
        string endTime = DateTime.Now.AddDays(-1).ToShortDateString() + " 23:59:59";
        DataSet ds = smBO.GetList(" ShortMessageSendTime between '" + startTime + "' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, GridView1);
        Cache["SMSList"] = ds.Tables[0];
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion
    #region 今天的短信记录
    protected void btn_Today_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTime.Now.ToShortDateString();
        txtDateB.Text = DateTime.Now.ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTime.Now.ToShortDateString() + " 00:00:00";
        string endTime = DateTime.Now.ToShortDateString() + " 23:59:59";
        DataSet ds = smBO.GetList(" ShortMessageSendTime between '" + startTime + "' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, GridView1);
        Cache["SMSList"] = ds.Tables[0];
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion
    #region 本周的短信记录
    protected void btn_ThisWeekend_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.GetThisWeekendFirst();
        txtDateB.Text = DateTimeHelper.GetThisWeekendLast();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        DataSet ds = smBO.GetList(" ShortMessageSendTime between '" + DateTimeHelper.GetThisWeekendFirst() + " 00:00:00' and '" + DateTimeHelper.GetThisWeekendLast() + " 23:59:59' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, GridView1);
        Cache["SMSList"] = ds.Tables[0];
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion
    #region 上周的短信记录
    protected void btn_LastWeekend_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.GetLastWeekendFirst();
        txtDateB.Text = DateTimeHelper.GetLastWeekendLast();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        DataSet ds = smBO.GetList(" ShortMessageSendTime between '" + DateTimeHelper.GetLastWeekendFirst() + " 00:00:00' and '" + DateTimeHelper.GetLastWeekendLast() + " 23:59:59' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, GridView1);
        Cache["SMSList"] = ds.Tables[0];
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion
    #region 本月的短信记录
    protected void btn_ThisMonth_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.FirstDayOfMonth().ToShortDateString();
        txtDateB.Text = DateTimeHelper.LastDayOfMonth().ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTimeHelper.FirstDayOfMonth().ToShortDateString() + " 00:00:00";
        string endTime = DateTimeHelper.LastDayOfMonth().ToShortDateString() + " 23:59:59";
        DataSet ds = smBO.GetList(" ShortMessageSendTime between '" + startTime + " ' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, GridView1);
        Cache["SMSList"] = ds.Tables[0];
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion
    #region 上月的短信记录
    protected void btn_LastMonth_Click(object sender, EventArgs e)
    {
        txtDateA.Text = DateTimeHelper.FirstDayOfPreviousMonth(DateTime.Now).ToShortDateString();
        txtDateB.Text = DateTimeHelper.LastDayOfPrdviousMonth(DateTime.Now).ToShortDateString();
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = ddl_EndHour.Items.Count - 1;
        string startTime = DateTimeHelper.FirstDayOfPreviousMonth(DateTime.Now).ToShortDateString() + " 00:00:00";
        string endTime = DateTimeHelper.LastDayOfPrdviousMonth(DateTime.Now).ToShortDateString() + " 23:59:59";
        DataSet ds = smBO.GetList(" ShortMessageSendTime between '" + startTime + " ' and '" + endTime + "' ");
        //ViewState["DataTable"] = ds.Tables[0];
        //Bind(ViewState["DataTable"] as DataTable, GridView1);
        Cache["SMSList"] = ds.Tables[0];
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion
    #region 显示所有的短信记录
    protected void btn_All_Click(object sender, EventArgs e)
    {
        txtDateA.Text = "";
        txtDateB.Text = "";
        ddl_StartHour.SelectedIndex = 0;
        ddl_EndHour.SelectedIndex = 0;
        DataSet ds = smBO.GetAllList();
        Cache["SMSList"] = ds.Tables[0];
        Bind(Cache["SMSList"] as DataTable, GridView1);
    }
    #endregion

    #region 绑定小时
    private void BindHour()
    {
        for (int i = 0; i < 24; i++)
        {
            ListItem liforstart = new ListItem(i.ToString("00"), i.ToString("00"));
            ListItem liforend = new ListItem(i.ToString("00"), i.ToString("00"));

            ddl_StartHour.Items.Add(liforstart);
            ddl_EndHour.Items.Add(liforend);
        }
    }
    #endregion
}
